﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogFileParser;

namespace LogFileViewer
{
    public partial class frmMain : Form
    {
        private string _logfilename = "";
        private LogFileParser.LogFileParser _lfp = null;
        private Dictionary<string, string> _excludeEvents = new Dictionary<string, string>();
        private string _filters = "";
        private List<LogFileItem> displayList = null;
        public frmMain()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            //foreach(string xyz in ConfigurationSettings.AppSettings.Keys
            string excludeEvents = ConfigurationSettings.AppSettings["EventExclude"];
            if (excludeEvents != null)
            {
                ProcessExcludeEventID(excludeEvents);
            }

            if (Environment.GetCommandLineArgs().Length > 1)
            {
                _logfilename = Environment.GetCommandLineArgs()[1];
            }

            if (_logfilename.Length > 0)
            {
                LoadLogFile(_logfilename);
            }
        }

        private void LoadLogFile(string logfilename)
        {
            _lfp = new LogFileParser.LogFileParser(logfilename,null,ExcludeItems);
            this.Text = logfilename;
            //dgItems.Columns.RemoveAt(0);
            int i = 0;
            foreach (string field in _lfp.Fields)
            {
                dgItems.Columns.Add(field,field);

                ++i;
            }
            foreach (LogFileItem lfi in _lfp.Items)
            {
                dgItems.Rows.Add(lfi.ToArray());
            }
            long totallines = _lfp.IncludedLines + _lfp.ExcludedLines;
            lblNumEntries.Text = "Log Entries: " + totallines.ToString();
            lblIncludedLine.Text = "Included: " + _lfp.IncludedLines.ToString();
            lblExcludedLines.Text = "Excluded: " + _lfp.ExcludedLines.ToString();
        }

        private void ProcessExcludeEventID(string idstring)
        {
            char[] delims = { ',' };
            string[] events = idstring.Split(delims);

            foreach(string evnt in events)
            {
                if (!_excludeEvents.ContainsKey(evnt))
                {
                    _excludeEvents.Add(evnt, evnt);
                }
            }
        }

        private bool ExcludeItems(LogFileItem lfi)
        {
            bool doNotExclude = true;

            if (_excludeEvents.ContainsKey(lfi["EventID"]))
            {
                doNotExclude = false;
            }

            return doNotExclude;
        }

        private void redisplay()
        {
            dgItems.Rows.Clear();
            foreach (LogFileItem lfi in displayList)
            {
                dgItems.Rows.Add(lfi.ToArray());
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (OpenLogFile.ShowDialog() == DialogResult.OK)
            {
                LoadLogFile(OpenLogFile.FileName);
            }
        }

        private void filterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filter filter = new Filter();

            filter.LogFile = _lfp;
            if (filter.ShowDialog() == DialogResult.OK)
            {
                _filters = filter.FilterProperty;
                displayList = filter.FilteredList;
                redisplay();
            }

        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _filters = "";
            displayList = _lfp.Items;
            redisplay();
        }

        private void dgItems_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

    }
}
